<template>
  <div class="adminList-wrap">
    <el-button-group
      style="margin-top: 10px; margin-bottom: 30px; float: right"
    >
      <el-button
        type="primary"
        style="margin-right: 20px"
        :icon="Plus"
        @click="fnRegister"
        v-permission="manage / site / register"
      >
        注册用户手环
      </el-button>
    </el-button-group>
    <el-input
      v-model="searchInput"
      style="width: 340px"
      size="large"
      placeholder="请输入搜索内容"
      :prefix-icon="Search"
      clearable
      @input="fnInput"
    />
    <el-table
      :data="datas"
      border
      style="width: 100%"
      :header-cell-style="{ background: '#f5f5f5', color: '#606266' }"
      :cell-style="cellStyle"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column label="场馆代码" min-width="80" align="center">
        <template #default="scope">{{
          scope.row.site_code ? scope.row.site_code : '-'
        }}</template>
      </el-table-column>
       <el-table-column label="区块代码" min-width="80" align="center">
        <template #default="scope">{{
          scope.row.area_code ? scope.row.area_code : '-'
        }}</template>
      </el-table-column>
      <el-table-column
        label="区块名称"
        min-width="160"
        align="center"
      >
        <template #default="scope">{{
          scope.row.area_name ? scope.row.area_name : '-'
        }}</template>
      </el-table-column>
      <el-table-column label="区块水深标记" min-width="160" align="center">
        <template #default="scope">{{
          scope.row.water_depth == 0 ? '水上区域':(scope.row.water_depth == 1 ? '浅水区':'深水区')
        }}</template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        min-width="160"
        align="center"
        sortable
        prop="created_time"
      >
        <template #default="scope">{{
          scope.row.created_time ? scope.row.created_time : '-'
        }}</template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        min-width="150"
        fixed="right"
      >
        <template #default="scope">
          <el-button type="warning" size="small" @click="fnModify(scope.row)">
            修改
          </el-button>
          <el-button type="danger" size="small" @click="fnDeleteOne(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--批量操作-->
    <el-button-group style="margin-top: 20px; margin-bottom: 30px">
      <el-button
        type="danger"
        plain
        size="middle"
        style="margin-right: 20px"
        @click="fnDeleteMore"
      >
        批量删除
      </el-button>
    </el-button-group>

    <!--分页-->
    <el-pagination
      class="page-warp"
      background
      layout="total,prev, pager, next, sizes"
      v-model:page-size="pageSize"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page="currentPage"
      @current-change="fnCurrentChange"
      @size-change="fnSizeChange"
    />
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, watch, computed } from 'vue'
import { useRouter,useRoute } from 'vue-router'
import { postData } from '@/http/request.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Search } from '@element-plus/icons-vue'
let router = useRouter()
let route = useRoute()
const routeParams = route.query.site_code; // 获取路由参数
let datas = ref([])
let res_datas = ref([])
let total = ref(0)
let currentPage = ref(1)
let pageSize = ref(10)
let searchInput = ref('') //搜索
const mutipleSelection = ref([]) //批量删除数组

//init
let getList = () => {
  postData('manage/site/area/list',{site_code:routeParams})
    .then((res) => {
      if (res.code == 0) {
        total.value = res?.datas?.areas?.length || 0
        res_datas.value = res?.datas?.areas || []
        datas.value = res_datas.value.slice(0, pageSize.value)
      } else if (res.code == -56055) {
        return
      } else {
        // ElMessage({
        //   type: 'error',
        //   message: 'error1',
        // })
      }
    })
    .catch((res) => {
      ElMessage({
        type: 'error',
        message: res,
      })
    })
}

//删除fn
let fn_del = (arr) => {
  ElMessageBox.confirm('确认删除场馆?', '删除确认', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      postData('manage/site/delete', {
        sites: arr,
      }).then((res) => {
        if (res.code == 0) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          currentPage.value = 1
          getList()
        } else {
          ElMessage({
            type: 'error',
            message: '删除失败',
          })
        }
      })
    })
    .catch(() => {})
}

//删除当前行
let fnDeleteOne = (dataRow) => {
  let arr = []
  arr.push(dataRow.site_code)
  fn_del(arr)
}

//批量删除
const handleSelectionChange = (val) => {
  mutipleSelection.value = val
}
let fnDeleteMore = () => {
  let arr = []
  mutipleSelection.value.forEach((row) => {
    arr.push(row.site_code)
  })
  if (arr.length == 0) {
    console.log(arr)
    ElMessage({
      type: 'error',
      message: '请选择要删除的场馆',
    })
  } else {
    fn_del(arr)
  }
}

//分页
let fnSizeChange = (data) => {
  pageSize.value = data
  currentPage.value = 1
  getList()
}
let fnCurrentChange = (data) => {
  let start = (data - 1) * pageSize.value
  currentPage.value = data
  datas.value = res_datas.value.slice(start, start + pageSize.value)
}

//search
let fnInput = (val) => {
  if (val) {
    // datas.value = datas.value.filter((item) => {
    //   return item.dev_id.includes(val)
    // })
    datas.value = datas.value.filter((item) => {
      return Object.keys(item).some((key) => {
        return String(item[key]).toLowerCase().indexOf(val) > -1
      })
    })
  } else {
    getList()
  }
  currentPage.value = 1
  total.value = datas.value.length
}

//去注册
let fnRegister = () => {
  router.push('/site/siteAreaRegister?site_code=' + routeParams)
}

//去修改
let fnModify = (data) => {
  router.push('/site/siteAreaModify?area_code=' + data.area_code)
}


onMounted(() => {
  getList()
})
</script>

<style scoped>
.adminList-wrap {
  width: 100%;
  height: 100%;
  position: relative;
}
.search-wrap {
  display: flex;
  justify-content: flex-start;
}

.adminList-wrap >>> .el-table__header-wrapper {
  background: red;
}
.adminList-wrap >>> .el-table__header {
  background: red;
}
.page-warp {
  float: right;
  margin-top: 10px;
}
</style>
